<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Good;
use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;

class GoodController extends Controller
{
    protected $name = '商品管理';
    use ModelForm;


    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Good::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');

            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->like('company', '公司名称');
                $filter->like('name', '联系人');
                //$filter->addFilter($filter->like('post_title', '标题'));
                $filter->equal('status', '类型')->select(Good::$status);

            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('title', '中文名称');
            $grid->column('rtitle', '日文名称');
            $grid->column('number', '库存');
            $grid->column('price', '价格');
            $states = [
                'on'  => ['value'=>1,'text' => '已上架', 'color' => 'primary'],
                'off' => ['value'=>0,'text' => '已下架', 'color' => 'default'],
            ];
            $grid->column('status', '状态')->switch($states);
            $grid->column('recommend', '属性')->select(Good::$recommend);


            $grid->column('updated_at', '更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Admin::form(Good::class, function (Form $form) {

            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '中文名称')->rules('required');
            $form->text('rtitle', '日文名称')->rules('required');
            $form->text('number', '库存')->rules('required');
            $form->currency('price', '价格')->rules('required');
            $form->image('thumb','列表图片')->uniqueName();
            $form->select('catid', '分类')->options(Category::selectOptions())->rules('required');
            $form->select('recommend', '属性')->options(Good::$recommend)->rules('required');
            $form->multipleImage('attachment', '中文图片')->uniqueName()->removable();
            $form->multipleImage('rattachment', '日文图片')->uniqueName()->removable();
            $form->embeds('parm', '中文参数', function ($form) {
                $form->text('parm1', '参数1')->rules('required');
                $form->text('parm2', '参数2');
                $form->text('parm3', '参数3');
                $form->text('parm4', '参数4');
            });
            $form->embeds('rparm', '日文参数', function ($form) {
                $form->text('rparm1', '参数1')->rules('required');
                $form->text('rparm2', '参数2');
                $form->text('rparm3', '参数3');
                $form->text('rparm4', '参数4');
            });

            $form->hasMany('goods_attributes', '属性参数', function (Form\NestedForm $form) {
                $form->text('name', '参数名称');
                $form->text('rname', '参数名称(日)');
                $form->text('unit', '单位');
                $form->text('runit', '单位(日)');
                $form->text('price', '价格');
                $form->text('number', '库存');
                $form->text('salled_number', '销量');
            });

            $form->editor('content', '中文参数');
            $form->editor('rcontent', '日文参数');
            $form->saving(function($form){
                if($form->status == 'on')
                {
                    $form->status = 1;
                }else{
                    $form->status = 0;
                }
            });
        });
    }
}
